﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MikroObject;
using System.Collections.Generic;

namespace Aktarim
{
    public partial class frmMain : XtraForm
    {

        Dictionary<CParams, int> sendTime = new Dictionary<CParams, int>();
        Dictionary<CParams, int> remainTime = new Dictionary<CParams, int>();
        Data dt = new Data();
        Synchronous senk = new Synchronous();

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Config.GetConfig.Save();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            sendTime.Add(CParams.AlisSevkGuncellemeSuresi, Convert.ToInt32(Config.GetConfig.GetValue(CParams.AlisSevkGuncellemeSuresi, "60")));
            sendTime.Add(CParams.StokUrunGuncellemeSuresi, Convert.ToInt32(Config.GetConfig.GetValue(CParams.StokUrunGuncellemeSuresi, "60")));
            sendTime.Add(CParams.TeminciGuncellemeSuresi, Convert.ToInt32(Config.GetConfig.GetValue(CParams.TeminciGuncellemeSuresi, "60")));

            remainTime.Add(CParams.AlisSevkGuncellemeSuresi, Convert.ToInt32(Config.GetConfig.GetValue(CParams.AlisSevkGuncellemeSuresi, "60")));
            remainTime.Add(CParams.StokUrunGuncellemeSuresi, Convert.ToInt32(Config.GetConfig.GetValue(CParams.StokUrunGuncellemeSuresi, "60")));
            remainTime.Add(CParams.TeminciGuncellemeSuresi, Convert.ToInt32(Config.GetConfig.GetValue(CParams.TeminciGuncellemeSuresi, "60")));

            timer1.Enabled = true;
            timer1.Start();

        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmAyarlar ayar = new frmAyarlar();
            ayar.ShowDialog(this);
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmSabitVeriAktar veri = new frmSabitVeriAktar();
            veri.ShowDialog(this);
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmSatisAktar satis = new frmSatisAktar();
            satis.ShowDialog(this);
        }

        private void AktarimaBasla(CParams tip)
        {
            Worker.WorkerSupportsCancellation = true;
            Worker.WorkerReportsProgress = true;
            Worker.RunWorkerAsync(tip);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            timer1.Enabled = false;
            AktarimKontrol();
        }

        private void AktarimKontrol()
        {
            foreach (CParams key in new List<CParams>(remainTime.Keys))
            {
                if (remainTime[key] <= 0)
                {
                    remainTime[key] = sendTime[key];
                    AktarimaBasla(key);
                    return;
                }
                else
                    remainTime[key] -= 1;
            }
            timer1.Enabled = true;
            timer1.Start();
        }

        private void Worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            switch ((CParams)e.Argument)
            {
                case CParams.StokUrunGuncellemeSuresi:
                    senk.StokAktar(dt.StokList());
                    break;
                case CParams.TeminciGuncellemeSuresi:
                    senk.CariAktar(dt.CariList());
                    break;
                case CParams.AlisSevkGuncellemeSuresi:
                    DateTime bastarih = DateTime.Now;
                    DateTime tarih = ExtensionMethods.ParseDate(Config.GetConfig.GetValue(CParams.LastupdateDate, "1900-1-1 0:0:0"));
                    senk.AlisAktar(dt.AlisList(tarih));
                    senk.DepoSevkAktar(dt.SevkList(tarih));
                    Config.GetConfig.Add(CParams.LastupdateDate, ExtensionMethods.DateToString(bastarih));
                    break;
            }
        }

        private void Worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
        }

        private void başlatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
        }

        private void durdurToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                timer1.Stop();
                timer1.Enabled = false;
                if (Worker.IsBusy)
                    Worker.CancelAsync();
                this.Show();
            }
            catch (Exception ex)
            {
                Logging.GetLog.ErrorLog(ex);
            }
        }

        private void çıkışToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                timer1.Stop();
                timer1.Enabled = false;
                if (Worker.IsBusy)
                    Worker.CancelAsync();
                Application.Exit();
            }
            catch (Exception ex)
            {
                Logging.GetLog.ErrorLog(ex);
            }
        }

        private void frmMain_Deactivate(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }
    }
}
